package dk.eclipse.android.badassapp.distance;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * @author jaw
 * 
 *         Receive updates on locations for items on map. The contract is an
 *         intent with: <br>
 * 
 *         Can create necessary intents for the calling client. I made these
 *         funtions because I am not sure about the "rules" when creating
 *         intents. To make it clear how the intents are expected - the code to
 *         create them is accessible here.
 * 
 *         uri - to look for further information <br>
 *         longitude <br>
 *         latitude <br>
 *         label <br>
 * 
 *         Flow:
 * 
 */
public class TargetUpdateReceiver extends BroadcastReceiver {
	public static final String NEW_OBJECT_ON_MAP = "dk.eclipse.android.badassapp.receivers.NEW_OBJECT_ON_MAP";
	public static final String UPDATE_OBJECT_ON_MAP = "dk.eclipse.android.badassapp.receivers.UPDATE_OBJECT_ON_MAP";
	public static final String REMOVE_OBJECT_ON_MAP = "dk.eclipse.android.badassapp.receivers.REMOVE_OBJECT_ON_MAP";
	public static final String ME_URI = "me";

	private static final String LOG_TAG = TargetUpdateReceiver.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(LOG_TAG, "message received");
		Bundle extras = intent.getExtras();
		String uri = extras.getString("uri");
		String label = extras.getString("label");
		Double longitude = extras.getDouble("longitude");
		Double latitude = extras.getDouble("latitude");

		String action = intent.getAction();
		if (NEW_OBJECT_ON_MAP.equals(action)) {
			DistanceActivity.newObjectOnMap(uri, latitude, longitude, label);
		} else if (UPDATE_OBJECT_ON_MAP.equals(action)) {
			DistanceActivity.updateObjectOnMap(uri, latitude, longitude);
		} else if (REMOVE_OBJECT_ON_MAP.equals(action)) {
			DistanceActivity.removeObjectOnMap(uri);
		}
	}

	// TODO JAW add image object for the map. Untill then, use defaults
	public static Intent createNewObjectIntent(String uri, String label,
			double latitude, double longitude) {
		Intent intent = new Intent(NEW_OBJECT_ON_MAP);

		// content://com.android.contacts/contacts/lookup/1281i31710fca0d8b2194/503

		// I could not get the intent filters to work when I had a uri as
		// data, so eventually I decideded to transfer the uri as a string.
		// intent.setData(myPerson);
		intent.putExtra("uri", uri);
		intent.putExtra("latitude", latitude);
		intent.putExtra("longitude", longitude);
		intent.putExtra("label", label);

		return intent;
	}

	public static Intent createRemoveObjectIntent(String uri) {
		Intent rv = new Intent(REMOVE_OBJECT_ON_MAP);
		rv.putExtra("uri", uri);
		return rv;
	}

}
